// 操作数库的对象  db
// 数据库：blog_database
// 数据表：pre_wish

// SELECT  字段
// FROM  数据表
// WHERE  条件
// INSERT  INTO 插入
// INTO   数据表
// UPDATE   数据表  更新
// DELETE   删除
// DELETE FROM 数据表

const db = require("../common/db/index");

module.exports = {
    // 发表祝福
    publish(wish_text, createAt, imgUrl, user_id, username) {
        return new Promise(async (resolve, reject) => {
            const sql = "insert into pre_wish (wish_text,createAt,imgUrl,user_id,username) values (?,?,?,?,?)";
            await db.query(sql, [wish_text, createAt, imgUrl, user_id, username]).then(
                (result) => {
                    resolve({
                        code: 200,
                        msg: "发表祝福成功",
                        result
                    });
                },
                (err) => {
                    reject({
                        code: -1,
                        msg: "发表祝福失败",
                        err,
                    })
                }
            )
        })
    },

    // 修改祝福
    modify(wish_text, createAt, id) {
        return new Promise((resolve, reject) => {
            const sql = "update pre_wish  set wish_text=? , createAt=? where id=?";
            db.query(sql, [wish_text, createAt, id])
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: "修改祝福成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "修改祝福失败",
                            err
                        })
                    }
                )
        })
    },
    // 删除祝福
    remove(id) {
        return new Promise((resolve, reject) => {
            let sql = "DELETE FROM pre_wish WHERE id = ?";
            db.query(sql, [id])
                .then(
                    result => {
                        resolve({
                            code: 200,
                            msg: "删除祝福成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "删除祝福失败",
                            err
                        })
                    }
                )
        })
    },

    // 获取祝福列表
    list(page_num, page_size) {
        return new Promise(async (resolve, reject) => {
            const rows = await db.query(`SELECT count(*) as total FROM pre_wish`);
            let page_num2 = ((parseInt(page_num) - 1) * parseInt(page_size))
            let page_size2 = (parseInt(page_size))
            let sql = `select * from pre_wish order by createAt DESC limit ${page_num2} , ${page_size2}`
            await db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            total: rows[0].total,
                            msg: "获取祝福列表成功",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "获取祝福列表失败",
                            err

                        })
                    }
                )
        })
    },

    // 搜索祝福
    search(keyword) {
        return new Promise(async (resolve, reject) => {
            let sql = `select * from pre_wish where wish_text like '%${keyword}%' or username like '%${keyword}%' or user_id like '%${keyword}%' order by createAt DESC`
            await db.query(sql)
                .then(
                    result => {
                        if (result.length > 0) {
                            resolve({
                                code: 200,
                                msg: "搜索祝福评论成功",
                                result
                            })
                        } else {
                            resolve({
                                code: -1,
                                msg: "没有搜索结果",
                                result
                            })

                        }
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "搜索失败",
                            err
                        })
                    }
                )
        })
    }
};
